<?php
namespace App\Service\H5;

class WhywService20250921 extends BaseService
{
    //列表
    public static function index($pas)
    {
        $db = self::dbConn(self::$dbYwWh)->where('del', 0);
        $res = $db->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)->where('id', $v->item_id)->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $taskOne = (array) self::dbConn(self::$dbYwTask)->where('id', $v->task_id)->first();
            $task_name = empty($taskOne) ? '' : $taskOne['title'];
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->orderBy('starthour', 'asc')
                ->get();
            $whList = [];
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $configOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $wh_title = empty($configOne) ? '' : $configOne['title'];
                    $whList[] = [
                        'id' => $vv->id, 'title' => $vv->title,
                        'start' => $vv->starthour, 'end' => $vv->endhour,
                        'delay' => $vv->delay, 'wh_title' => $wh_title,
                    ];
                }
            }
            $arys[] = [
                'id' => $v->id, 'item_id' => $v->item_id, 'item_name' => $item_name,
                'task_id' => $v->task_id, 'task_name' => $task_name, 'whList' => $whList,
            ];
        }
        return self::toback(0, '', $arys);
    }

    //详情
    public static function getOne($id)
    {
        $res = self::dbConn(self::$dbYwWh)
            ->select('id', 'item_id', 'task_id')
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $itemAry = self::dbConn(self::$dbConfigItem)
            ->select('id', 'title')
            ->where('del', 0)
            ->get();
        $taskAry = self::dbConn(self::$dbYwTask)
            ->select('id', 'title')
            ->where('del', 0)
            ->get();
        //开始时间
        $arys = [
            'data' => $res,
            'itemList' => $itemAry,
            'taskList' => $taskAry,
        ];
        return self::toback(0, '', $arys);
    }

    //新增
    public static function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwWh)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
//
//    //删除
//    public static function del($id)
//    {
//        $res = self::dbConn(self::$dbConfigItem)->where('id', $id)->first();
//        if (empty($res)) {
//            return self::toback(-2, '数据不存在');
//        }
//        $res = self::dbConn(self::$dbStDepart)
//            ->where('id', $id)
//            ->update(['del' => 1, 'deltime' => toDate()]);
//        if (!$res) {
//            return self::toback(-1, 'error');
//        }
//        return self::toback();
//    }

    //审核列表
    public static function authList()
    {
        $db = self::dbConn(self::$dbYwWh)
            ->where('del', 0)
            ->where('status', 2);
        $res = $db->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)->where('id', $v->item_id)->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $taskOne = (array) self::dbConn(self::$dbYwTask)->where('id', $v->task_id)->first();
            $task_name = empty($taskOne) ? '' : $taskOne['title'];
            $whOne = (array) self::dbConn(self::$dbConfigWorkHour)->where('id', $v->wh_id)->first();
            $wh_name = empty($whOne) ? '' : $whOne['title'];
            $delay = (strtotime($v->endhour) - strtotime($v->starthour)) / 60;
            $arys[] = [
                'id' => $v->id, 'item_id' => $v->item_id, 'item_name' => $item_name,
                'task_id' => $v->task_id, 'task_name' => $task_name,
                'wh_id' => $v->wh_id, 'wh_name' => $wh_name,
                'starthour' => $v->starthour, 'endhour' => $v->endhour,
                'delay' => $delay.'分钟',
            ];
        }
        return self::toback(0, '', $arys);
    }

    //审核动作
    public static function authSet($id, $status, $remark)
    {
        $res = (array) self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '数据不存在');
        }
        if ($res['status'] > 2) {
            return self::toback(-2, '状态错误');
        }
        $statusNew = $status==1 ? 3 : 4;
        self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->update(['status' => $statusNew]);
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'type' => $statusNew, 'remark' => $remark, 'addtime' => toDate(),
        ]);
        return self::toback();
    }
}